package com.open.capacity.form.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.commons.Result;
import com.open.capacity.form.dao.FormSchemeDao;
import com.open.capacity.form.dao.FormSchemeinfoDao;
import com.open.capacity.form.model.FormRelation;
import com.open.capacity.form.model.FormScheme;
import com.open.capacity.form.model.FormSchemeinfo;
import com.open.capacity.form.service.CustomerFormService;
import com.open.capacity.form.vo.CustomerFormVo;
import com.open.capacity.form.vo.SchemeVo;
import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.utils.PageUtil;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Slf4j
@Service
public class CustomerFormServiceImpl implements CustomerFormService {

    @Autowired
    private FormSchemeinfoDao schemeinfoDao;

    @Autowired
    private FormSchemeDao schemeDao;



    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(FormSchemeinfo schemeinfo) {
        schemeinfo.setCreateTime(new Date());
        schemeinfoDao.save(schemeinfo);
        log.info("保存表单：{}", schemeinfo);
    }

    @Override
    public void updateScheme(String id, String schemeId) {
        FormSchemeinfo schemeinfo = schemeinfoDao.findById(id);
        schemeinfo.setSchemeId(schemeId);
        schemeinfoDao.updateByOps(schemeinfo);
        log.info("修改状态：{}", schemeinfo);
    }

    @Override
    public void updateStatus(String id, Integer status) {
        FormSchemeinfo schemeinfo = schemeinfoDao.findById(id);
        schemeinfo.setStatus(status);
        schemeinfoDao.updateByOps(schemeinfo);
        log.info("修改状态：{}", schemeinfo);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(FormSchemeinfo schemeinfo) {
        schemeinfoDao.updateByOps(schemeinfo);
        log.info("修改表单：{}", schemeinfo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        FormSchemeinfo schemeinfo = schemeinfoDao.findById(id);
        schemeinfoDao.delete(id);
        schemeDao.delete(id);
        log.info("删除表单：{}", schemeinfo);
    }


    @Override
    public SchemeVo findById(String id) {
        SchemeVo vo = new SchemeVo();
        FormSchemeinfo schemeinfo = schemeinfoDao.findById(id);
        FormScheme scheme = schemeDao.findById(schemeinfo.getSchemeId());
        vo.setScheme(scheme);
        vo.setSchemeinfo(schemeinfo);
        return vo;
    }

    @Override
    public List<FormSchemeinfo> findSchemeinfos(Map<String, Object> params) {
        List<FormSchemeinfo> list = schemeinfoDao.findList(params);
        return list;
    }

    @Override
    public PageResult<FormScheme> pageSchemes(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<FormScheme>  list = schemeDao.findList(params);
        PageInfo<FormScheme> pageInfo = new PageInfo(list);

        return PageResult.<FormScheme>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();

    }

    @Override
    public PageResult<FormSchemeinfo> pageSchemeinfos(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<FormSchemeinfo>  list = schemeinfoDao.findList(params);
        PageInfo<FormSchemeinfo> pageInfo = new PageInfo(list);

        return PageResult.<FormSchemeinfo>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result saveOrUpdate(CustomerFormVo schemeVo) {
        int i = 0;
        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();
        if (StringUtils.isNotEmpty(schemeVo.getId())) {
            FormSchemeinfo schemeinfo = schemeinfoDao.findById(schemeVo.getId());
            String id = UUIDUtils.getOriginalUUIDWithoutBar();
            Date now = new Date();
            if (schemeinfo != null) {
                FormSchemeinfo newData = schemeVo.getSchemeinfo();
                schemeinfo.setName(newData.getName());
                schemeinfo.setType(newData.getType());
                schemeinfo.setCategory(newData.getCategory());
                schemeinfo.setUrlAddress(newData.getUrlAddress());
                schemeinfo.setSchemeId(id);
                schemeinfo.setUpdateUserid(loginUser.getId());
                schemeinfo.setUpdateUsername(loginUser.getRealName());
                schemeinfo.setUpdateTime(now);
                i = schemeinfoDao.updateByOps(schemeinfo);
            }

            FormScheme scheme = new FormScheme();
            scheme.setId(id);
            scheme.setCompanyId(schemeVo.getCompanyId());
            scheme.setSchemeinfoId(schemeinfo.getId());
            scheme.setType(schemeVo.getType());
            scheme.setScheme(schemeVo.getScheme());
            scheme.setCreateUserid(loginUser.getId());
            scheme.setCreateUsername(loginUser.getRealName());
            scheme.setCreateTime(now);
            schemeDao.save(scheme);

        } else {
            Date now = new Date();
            String id = UUIDUtil.uuid();
            FormSchemeinfo schemeinfo = schemeVo.getSchemeinfo();

            schemeinfo.setId(id);
            schemeinfo.setCompanyId(schemeVo.getCompanyId());
            schemeinfo.setSchemeId(id);
            schemeinfo.setCreateUserid(loginUser.getId());
            schemeinfo.setCreateUsername(loginUser.getRealName());
            schemeinfo.setCreateTime(now);
            schemeinfo.setUpdateTime(now);
            schemeinfo.setStatus(1);

            FormScheme scheme = new FormScheme();
            scheme.setId(id);
            scheme.setCompanyId(schemeVo.getCompanyId());
            scheme.setSchemeinfoId(id);
            scheme.setType(schemeVo.getType());
            scheme.setScheme(schemeVo.getScheme());
            scheme.setCreateUserid(loginUser.getId());
            scheme.setCreateUsername(loginUser.getRealName());
            scheme.setCreateTime(now);
            schemeDao.save(scheme);
            i = schemeinfoDao.save(schemeinfo);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }
}
